<template>
  <el-card>
    <div slot="header" class="card-header">
      <span>编辑商品</span>
      <div class="button-group">
        <el-button
            type="primary"
            size="small"
            @click="saveMerchant"
        >
          保存
        </el-button>
      </div>
    </div>

    <el-form
        :model="formData"
        label-width="120px"
        class="merchant-form"
    >
      <el-form-item label="商品ID" prop="id">
        <el-input v-model="formData.id" disabled></el-input>
      </el-form-item>

      <el-form-item label="商品名" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>

      <el-form-item label="价格" prop="price">
        <el-input v-model="formData.price"></el-input>
      </el-form-item>

      <el-form-item label="商家描述" prop="description">
        <el-input
            v-model="formData.description"
            type="textarea"
            placeholder="请输入商家描述"
        ></el-input>
      </el-form-item>

    </el-form>
  </el-card>
</template>
<script setup>
import {Plus} from "@element-plus/icons-vue";
import {onMounted, reactive,ref} from "vue";
import {ElMessage} from "element-plus";
import server from "../utils/request.js";
import router from "../router/index.js";
import {useRoute}  from "vue-router";

const id = useRoute().query.id

const formData = ref({
  id: '',
  merchantId: '',
  name: '',
  price: '',
  description: '',
  status: ''
})


const saveMerchant = async () => {
    const { data } = await server.put(`/goods/`, formData.value);
    if (data.code === 200) {
      //跳转至商品列表
      router.push("/goodsList")
      ElMessage.success('保存成功');
    } else {
      ElMessage.error(`保存失败：${data.msg}`);
    }
}
const fetchMerchantDetail = async () => {
  try {
    let {data:data} = await server.get(`goods/${id}`)
    Object.assign(formData.value,data.data)
  } catch (error) {
    ElMessage.error('获取商家详情失败')
  }
}

// 页面加载时获取数据
onMounted(() => {
  fetchMerchantDetail()
})

</script>
<style scoped>
.merchant-page {
  padding: 20px;
}

.page-card {
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.edit-btn {
  margin-left: auto;
}

.button-group {
  display: flex;
  gap: 10px;
}

.merchant-form {
  margin-top: 20px;
}

.avatar-uploader {
  display: inline-block;
}

.upload-placeholder {
  width: 200px;
  height: 150px;
  border: 1px dashed #ccc;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.upload-text {
  margin-top: 8px;
  color: #666;
  font-size: 14px;
}
</style>